package com.itheima.controller.orders;

import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.Orders;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

@WebServlet("/orders/*")
public class OrdersServlet extends BaseServlet {

    /**
     * 查询所有订单-产品
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Orders> ordersList=null;
        //获得分页信息
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        String name = req.getParameter("name");
        if (name != null ){
            ordersList = ordersService.findAllByName(pageNum,pageSize,name);
        }else {
            ordersList = ordersService.findAll(pageNum,pageSize);

        }
        PageInfo<Orders> pageInfo = new PageInfo<Orders>(ordersList);
        req.setAttribute("page", pageInfo);
        req.getRequestDispatcher("/pages/orders/orders-list.jsp").forward(req, resp);
    }

    /**
     * 通过id删除
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteByOIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获得复选框的id
        String ids = req.getParameter("ids");

//        System.out.println(ids);
//        if (true){
//            return;
//        }
//        boolean flag = ordersService.deleteByIds(ids);
//        //把flag转换成json格式
//        String json = mapper.writeValueAsString(flag);
//
//        resp.getWriter().write(json);
        ordersService.deleteByOIds(ids);
        deleteByIds(req, resp, ids);
    }

    protected void deleteByIds(HttpServletRequest req, HttpServletResponse resp,String ids) throws ServletException, IOException {
         boolean flag = ordersService.deleteByIds(ids);

        //把flag转换成json格式
        String json = mapper.writeValueAsString(flag);
        resp.getWriter().write(json);
    }


    /**
     * 通过id查询订单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取id
        String id = req.getParameter("id");
//        System.out.println(id);
        List<Orders> orders = ordersService.findById(id);
        req.setAttribute("orders", orders);
        req.getRequestDispatcher("/pages/orders/orders-show.jsp").forward(req, resp);
    }

    /**
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void downLoadOrders(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //返回的数据类型为文件xlsx类型
        resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = new String("订单表.xlsx".getBytes(), "iso8859-1");
        resp.addHeader("Content-Disposition", "attachment;fileName=" + fileName);

        //生成报告的文件，然后传递到前端页面
        ByteArrayOutputStream os = ordersService.getReport();
        //获取产生响应的流对象
        ServletOutputStream sos = resp.getOutputStream();
        //将数据从原始的字节流对象中提取出来写入到servlet对应的输出流中
        os.writeTo(sos);
        //将输出流刷新
        sos.flush();
        os.close();
    }
}
